package cn.school.controller.basice;

import cn.school.basics.entity.MajorSubject;
import cn.school.basics.service.BaseSubjectService;
import cn.school.basics.vo.BaseSpaceLocationVO;
import cn.school.basics.vo.BaseSubjectVO;
import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = "专业接口")
@RequestMapping("/spaceSubject")
public class BaseSubjectController {

    @Resource
    private BaseSubjectService baseSubjectService;

    @ApiOperation("findBaseSubjectPageList => 分页查询位置信息")
    @PostMapping(value = "/findBaseSubjectPageList")
    public CommonResult<CommonPage<BaseSubjectVO>> findBaseSubjectPageList(@RequestBody BaseSubjectVO vo) {
        IPage<BaseSubjectVO> pageList = baseSubjectService.findSubjectPageList(vo);
        return CommonResult.success(CommonPage.restPage(pageList));
    }

    @ApiOperation("addOrUpdate => 新增or修改")
    @PostMapping(value = "/addOrUpdateSubject")
    public CommonResult<String> addOrUpdateSubject(@RequestBody BaseSubjectVO vo) {
        int num = baseSubjectService.addOrUpdateSubject(vo);
        if(num==-1){
           return CommonResult.failed("专业编码重复");
        }
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed("提交失败");
    }

    @ApiOperation("delete => 删除")
    @PostMapping(value = "/deleteBaseSubject")
    public CommonResult<CommonPage<BaseSpaceLocationVO>> deleteBaseSubject(@RequestBody List<Long> ids) {
        int num = baseSubjectService.deleteSubjectById(ids);
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed("提交失败");
    }

    @ApiOperation("get => 查询专业关联课程")
    @GetMapping(value = "/getMajorAllSubj/{majorId}")
    public CommonResult getMajorAllSubj(@PathVariable("majorId")Long majorId){
        List<MajorSubject> majorSubjectList = baseSubjectService.getMajorAllSubj(majorId);
        return CommonResult.success(majorSubjectList);
    }

    @GetMapping("/getClassAllSubj/{classId}")
    public CommonResult<List<MajorSubject>> getClassAllSubj(@PathVariable("classId")String classId){
        List<MajorSubject> list = baseSubjectService.getClassAllSubj(classId);
        return CommonResult.success(list);
    }

}
